package net.koodar.forge.system.menu.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import net.koodar.forge.common.domain.dto.Response;
import net.koodar.forge.system.menu.model.dto.ManuParamDTO;
import net.koodar.forge.system.menu.model.vo.MenuVO;
import net.koodar.forge.common.domain.dto.MultiResponse;
import net.koodar.forge.log.annotation.OperateLog;
import net.koodar.forge.system.menu.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 菜单接口
 *
 * @author liyc
 */
@OperateLog
@Tag(name = "MenusController", description = "系统管理-菜单")
@RestController
@RequiredArgsConstructor
public class MenusController {

	private final MenuService menuService;

	/**
	 * 获取当前用户菜单，用于前端动态生成菜单
	 *
	 * @return 菜单
	 */
	@Operation(summary = "获取当前用户菜单列表")
	@GetMapping("/menus")
	public MultiResponse<MenuVO> getMenus() {
		return menuService.getMenus();
	}

	/**
	 * 获取所有权限
	 * @return 权限tree
	 */
	@Operation(summary = "获取所有权限")
	@GetMapping("/menu/tree")
	public Response listAllPermission() {
		return menuService.listTree();
	}

	/**
	 * 添加权限
	 *
	 * @param manuParamDTO 权限参数
	 * @return 操作结果
	 */
	@Operation(summary = "添加权限")
	@PostMapping("/menu/add")
	@PreAuthorize("@forgeAuth.checkPermission('system:permission:add')")
	public Response addPermission(@RequestBody ManuParamDTO manuParamDTO) {
		return menuService.addMenu(manuParamDTO);
	}

	/**
	 * 更新权限
	 *
	 * @param manuParamDTO 权限参数
	 * @return 操作结果
	 */
	@Operation(summary = "更新权限")
	@PostMapping("/menu/update")
	@PreAuthorize("@forgeAuth.checkPermission('system:permission:update')")
	public Response updatePermission(@RequestBody ManuParamDTO manuParamDTO) {
		return menuService.updateMenu(manuParamDTO);
	}

	/**
	 * 删除权限
	 *
	 * @param menuId 权限Id
	 * @return 操作结果
	 */
	@Operation(summary = "删除权限")
	@PostMapping("/menu/delete")
	@PreAuthorize("@forgeAuth.checkPermission('system:permission:delete')")
	public Response deletedPermission(@RequestParam Long menuId) {
		return menuService.deleteMenu(menuId);
	}

}
